package com.xunk.equipment.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.config
 * @ClassName: ThreadConfig
 * @Author: yangwendong
 * @Description:
 * @Date: 2021/3/29 11:34
 * @Version: 1.0
 */
@Configuration
public class ThreadConfig {

    @Bean(name = "asyncMqttTaskExecutor")
    public TaskExecutor asyncMqttTaskExecutor() {
        // TODO 开发环境线程数设置小一些
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5); // 50
        // 设置最大线程数
        executor.setMaxPoolSize(5); // 100
        // 设置队列容量
        executor.setQueueCapacity(50); // 1024
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("async-mqtt-task-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
//        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Bean(name = "asyncServiceTaskExecutor")
    public TaskExecutor asyncServiceTaskExecutor() {
        // TODO 开发环境线程数设置小一些
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5); // 50
        // 设置最大线程数
        executor.setMaxPoolSize(5); // 100
        // 设置队列容量
        executor.setQueueCapacity(50); // 1024
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("async-service-task-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
//        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Bean(name = "asyncServiceTaskExecutor2")
    public TaskExecutor asyncServiceTaskExecutor2() {
        // TODO 开发环境线程数设置小一些
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5); // 50
        // 设置最大线程数
        executor.setMaxPoolSize(5); // 100
        // 设置队列容量
        executor.setQueueCapacity(50); // 1024
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("async-service-task2-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
//        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }
}
